package vn.lumi.smarthome.network;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import vn.lumi.smarthome.ui.login.LoginActivity;

import android.content.Context;
import android.os.Parcel;
import android.util.Log;

public class Login extends AppMsg {

	//constants
	private final static int LENGTH = 44;
	
	//attributes
	private int Last_Session_id = 0;
	private String UserName = null;
	private String Password = null;
	private int Config_ver = 0;
	private int Version_Hc = 0x01;
	
	public Login(int LastSid, String Uname, String Pwd, int ConfigVer) {
		super(NetMessage.CMD_APP_LOGIN, 0, LENGTH, 0, 0);
		// TODO Auto-generated constructor stub
		Last_Session_id = LastSid;
		UserName = Uname;
		Password = Pwd;
		Config_ver = ConfigVer;
	}

	@Override
	public byte[] generateByteArray() {
		// TODO Auto-generated method stub
		byte[] result;
		if(LoginActivity.isConfigVersion)
		result = new byte[LENGTH];
		else result = new byte[LENGTH - 1];
		result[0] = (byte) this.Cmd_id;
		result[1] = (byte) this.Last_Session_id;

		byte[] name = null;
		try {
			name = UserName.getBytes("UTF-16BE");
			Log.e("ban tin","name: " + Arrays.toString(name));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i = 0; i<20; i++){
			if(i<name.length) result[2+i] = name[i];
			else result[2+i] = (byte) 0x00;
		}
		
		byte[] pass = null;
		try {
			pass = Password.getBytes("UTF-16BE");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i = 0; i<20; i++){
			if(i<pass.length) result[22+i] = pass[i];
			else result[22+i] = (byte) 0x00;
		}
		
		result[42] = (byte) Config_ver;
		if(LoginActivity.isConfigVersion)
		result[43] =(byte)Version_Hc;
		return result;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doIfSendSuccessful(Context context) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doIfSendFail(Context context) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void readFromParcel(Parcel in) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isCorrectReply(byte[] input, int length) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#isCorrectBusy(byte[], int)
	 */
	@Override
	public boolean isCorrectBusy(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result =false;
		HC_ACK_BUSY ack_busy = new HC_ACK_BUSY();
		if(ack_busy.isCorrectFormatBusy(input, length)){
			ack_busy.parseMessage(input, length);
			if(ack_busy.getMsg_id() == this.Msg_id) result = true;
		}
		return result; 
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#doIfHcBusying()
	 */
	@Override
	public void doIfHcBusying() {
		// TODO Auto-generated method stub
		
	}

}
